package com.wecheir.controller;

import com.wecheir.dao.MessageDao;
import com.wecheir.dao.UserDao;
import com.wecheir.entity.Message;
import com.wecheir.entity.User;
import com.wecheir.service.MessageService;
import com.wecheir.service.UserService;
import com.wecheir.utils.ResponseUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息管理controller层
 */
@Controller
@RequestMapping("/app/message")
public class MessageController {

    @Autowired
    MessageService messageService;

    @Autowired
    UserService userService;

    @Autowired
    MessageDao messageDao;

    @GetMapping(value = "/list")
    public String list(){
        return "admin/message/list";
    }

//    @RequiresPermissions("user:message:list")
//    @PostMapping("list")
//    @ResponseBody
//    public PageData<Message> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
//                                  @RequestParam(value = "limit",defaultValue = "10")Integer limit,
//                                  ServletRequest request){
////        Map map = WebUtils.getParametersStartingWith(request, "s_");
//        PageData<Message> messagePageData = new PageData<>();
////        QueryWrapper<Message> messageWrapper = new QueryWrapper<>();
////        //加条件
////        messageWrapper.eq("del_flag",false);
////        messageWrapper.eq("to_user",MySysUser.id());
////        if(!map.isEmpty()){
////            String keys = (String) map.get("key");
////            if(StringUtils.isNotBlank(keys)) {
////                messageWrapper.like("name", keys);
////            }
////        }
////        IPage<Message> messagePage = messageService.page(new Page<>(page,limit),messageWrapper);
////        messagePageData.setCount(messagePage.getTotal());
////        messagePageData.setData(messagePage.getRecords());
//        /*messagePageData.setData(setUserToMessage(messagePage.getRecords()));*/
//        return messagePageData;
//    }

//    @RequiresPermissions("user:message:list")
    @PostMapping("/new")
    @ResponseBody
    public ResponseUtils getNew(String uid,Integer page,Integer limit){
        ModelMap modelMap=new ModelMap();
        List<Message> messageListTop= messageDao.selectMessageList(uid,(page-1)*limit,limit);

//        int messageListCount=messageService.selectMessageListCount(MySysUser.id());
        int messageListCount=messageListTop.size();

        modelMap.put("messageListTop",messageListTop);
        modelMap.put("messageListCount",messageListCount);
        return ResponseUtils.setSuccess(modelMap);
    }


    @RequestMapping(value = "/add")
    @ResponseBody
    public ResponseUtils add(Message message,String uid){
        User toUserEn=userService.findUserById(message.getToUser());
        if(null==toUserEn){
            return ResponseUtils.setFailure("该用户不存在,不能发送消息!");
        }
        if(StringUtils.isBlank(message.getTitle())){
            return ResponseUtils.setFailure("消息标题不能为空");
        }
        if(StringUtils.isBlank(message.getMessageType())){
            return ResponseUtils.setFailure("消息类型不能为空");
        }
        if(StringUtils.isBlank(message.getToUser())){
            return ResponseUtils.setFailure("接收人不能为空");
        }
        messageService.saveMessage(message,uid);
        return ResponseUtils.setSuccess("");
    }

    /**
     * 群发消息
     * @param modelMap
     * @return
     */
    @GetMapping("/addMore")
    public String addMore(ModelMap modelMap, @RequestParam(value = "roleId",required = false) String roleId){
        List<User> userList=userService.findAllUser();
        List<Message> messsageList= messageService.selectAll();
        modelMap.put("userList",userList);
        modelMap.put("messsageList",messsageList);
        return "admin/message/addMore";
    }

    @RequiresPermissions("user:message:add")
    @PostMapping("/addMore")
    @ResponseBody
    public ResponseUtils addMore(@RequestBody Message message,String uid){
        User toUserEn=userService.findUserById(message.getToUser());
        if(null==toUserEn){
            return ResponseUtils.setFailure("该用户不存在,不能发送消息!");
        }
//        message.setLooked("未读");
//        message.setCreateId(MySysUser.id());
//        message.setCreateDate(new Date());
//        message.setUpdateDate(new Date());
//        message.setUpdateId(MySysUser.id());
//        message.setCreateName(toUserEn.getNickName());
//        message.setToUser(toUserEn.getId());
        if(StringUtils.isBlank(message.getTitle())){
            return ResponseUtils.setFailure("消息标题不能为空");
        }
        if(StringUtils.isBlank(message.getMessageType())){
            return ResponseUtils.setFailure("消息类型不能为空");
        }
        if(StringUtils.isBlank(message.getToUser())){
            return ResponseUtils.setFailure("接收人不能为空");
        }
        messageService.saveMessage(message, uid);
        return ResponseUtils.setSuccess("");
    }


    @RequiresPermissions("user:message:add")
    @GetMapping("/read")
    @ResponseBody
    public ResponseUtils read(String id){
        Message message = messageService.getMessageById(id);
        message.setLooked("已读");
//        message.setId(id);
        messageService.updateMessage(message);
        return ResponseUtils.setSuccess("消息已读");
    }

    @RequiresPermissions("user:message:add")
    @PostMapping("/readSome")
    @ResponseBody
    public ResponseUtils readSome(@RequestBody List<Message> messages){
        for (Message message:messages){
            message.setLooked("已读");
            messageService.updateMessage(message);
        }

        return ResponseUtils.setSuccess("这些消息已读");
    }

    @GetMapping("/edit")
    public String edit(String id, ModelMap modelMap){
        Message message = messageService.getMessageById(id);
        modelMap.put("message",message);
        return "admin/message/edit";
    }

    @RequiresPermissions("user:message:edit")
    @PostMapping("edit")
    @ResponseBody
    public ResponseUtils edit(@RequestBody Message message){
//        if(StringUtils.isBlank(message.getId())){
//            return ResponseUtils.setFailure("消息ID不能为空");
//        }
        if(StringUtils.isBlank(message.getTitle())){
            return ResponseUtils.setFailure("消息标题不能为空");
        }
        if(StringUtils.isBlank(message.getMessageType())){
            return ResponseUtils.setFailure("消息类型不能为空");
        }
        if(StringUtils.isBlank(message.getToUser())){
            return ResponseUtils.setFailure("接收人不能为空");
        }
//        Message oldMessage = messageService.getMessageById(message.getId());
     /*   if(!oldMessage.getName().equals(message.getName())){
            if(messageService.getMessageNameCount(message.getName())>0){
                return ResponseEntity.failure("消息名称已存在");
            }
        }*/
        messageService.updateMessage(message);
        return ResponseUtils.setSuccess("操作成功");
    }

    @RequiresPermissions("user:message:delete")
    @PostMapping("/delete")
    @ResponseBody
    public ResponseUtils delete(@RequestParam(value = "id",required = false)String id){
        if(StringUtils.isBlank(id)){
            return ResponseUtils.setFailure("消息ID不能为空");
        }
        Message message = messageService.getMessageById(id);
//        message.setId(id);
        messageService.deleteMessage(message);
        return ResponseUtils.setSuccess("操作成功");
    }

    @RequiresPermissions("user:message:delete")
    @PostMapping("/deleteSome")
    @ResponseBody
    public ResponseUtils deleteSome(@RequestBody List<Message> messages){
        if(messages == null || messages.size()==0){
            return ResponseUtils.setFailure("请选择需要删除的消息");
        }
        for (Message r : messages){
            messageService.deleteMessage(r);
        }
        return ResponseUtils.setSuccess("操作成功");
    }
}
